<template>
	<view class="goods-detail-container">
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" class="swiper-box">
			<swiper-item v-for="(item, i) in goodsInfo.pics" :key = "i" class="swiper-item" @click="preBigImgClick(i)">
				<image :src="item.pics_big" mode="aspectFit"></image>
			</swiper-item>
		</swiper>
	
		<view class="goods-info-box">
			<view class="price">￥{{goodsInfo.goods_price}}</view>
			<view class="goods-info-body">
				<view class="name">{{goodsInfo.goods_name}}</view>
				<view class="favi">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<view class="yf">快递：免运费---{{cartArr.length}}</view>
		</view>
		
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
		
		<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
	</view>
</template>

<script>
	//从vuex中按需导出mapState辅助方法 map	Mutations
	import { mapState, mapMutations, mapGetters } from 'vuex'

	export default {
		computed: {
			//调用 mapState方法，把m_cart模块中的cart数组映射到当前页面中，作为计算属性来使用
			//...mapState（‘模块的名称’, ['要映射的数据名称1'，‘要映射的数据名称2’]）
			//...这是展开运算符，把它展开交个计算属性
			...mapState('m_cart', ['cartArr']),
			//把m_cart模块中名称为total的getter映射到当前页面中使用
			...mapGetters('m_cart', ['total'])
		},
		
		//监听属性
		watch: {
			//使用普通函数的形式的watch监听器，在页面首次加载后不会调用
			// total(newVal) {
			// 	const findResult = this.options.find(x => x.text === '购物车')
			// 	if (findResult) {
			// 		findResult.info = newVal
			// 	}
			// }
			
			//使用对象的形式定义watch监听器
			total: {
				//handler属性用来定义侦听器的function处理函数
				handler(newVal) {
					const findResult = this.options.find(x => x.text === '购物车')
					if (findResult) {
						findResult.info = newVal
					}
				},
				//immediate属性用来声明此侦听器，是否在页面首次加载完毕后立即调用
				immediate: true
			},
			
		},
		
		data() {
			return {
				goodsInfo: {},
				 options: [{
						icon: 'headphones',
						text: '客服'
					}, {
						icon: 'cart',
						text: '购物车',
						info: 0
					}],
				buttonGroup: [{
					  text: '加入购物车',
					  backgroundColor: '#ff0000',
					  color: '#fff'
					},
					{
					  text: '立即购买',
					  backgroundColor: '#ffa200',
					  color: '#fff'
					}]
			};
		},
		
		onLoad(option) {
			const goods_id = option.goods_id
			this.getGoodsDetail(goods_id)
		},
		
		methods: {
			async getGoodsDetail(goods_id) {
				const {data: res} = await uni.$http.get('/api/public/v1/goods/detail', { goods_id })
				if (res.meta.status !== 200) return uni.$showMsg()
				
				//res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(/webp/g, 'jpg')
				//去掉图片之间的空白
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block:" ').replace(/webp/g, 'jpg')
				this.goodsInfo = res.message
				
			},
			
			preBigImgClick(i) {
				let urls = this.goodsInfo.pics.map(x => x.pics_big)
				uni.previewImage({
					current:i,
					urls:urls
				})
			},
			
			onClick (e) {
				if (e.index === 1) { 
					//购物车
					
				}
			},
			buttonClick (e) {
				if (e.index === 0) {
					//加入购物车
					const goods = {
						goods_id: this.goodsInfo.goods_id,
						goods_name: this.goodsInfo.goods_name,
						goods_price: this.goodsInfo.goods_price,
						goods_count: 1,
						goods_small_logo: this.goodsInfo.goods_small_logo,
						goods_state: true
					}
					this.addToCart(goods)
				} else if (e.index === 1) {
					//立即购买
					
				}
			},
			
			//把 m_cart模块中的addToCart方法映射到当前页面使用
			...mapMutations('m_cart', ['addToCart']),
			
			
		}
	}
</script>

<style lang="scss">
.swiper-box {
	height: 200px;
	
	image {
		width: 100%;
		height: 100%;
	}
}

.goods-info-box {
	padding: 5px;
	
	.price {
		color: #fc0107;
		font-size: 20px;
	}
	
	.goods-info-body {
		display: flex;
		align-items: center;
		justify-content: space-between;
		
		.name {
			font-size: 15px;
			margin-right: 5px;
		}
		
		.favi {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			border-left: 1px solid #efefef;
			width: 120px;
			font-size: 12px;
			color: gray;
		}
	}
	
	.yf {
	  font-size: 12px;
	  color: gray;
	  margin: 10px 0;
	}
}

.uni-goods-nav {
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
}

.goods-detail-container {
    padding-bottom: 50px;
 }
 
</style>
